<?php

namespace app\shop\controller\business;

use think\Controller;
use app\common\library\Email;

class send extends Controller
{
    protected $EmsModel = null;

    protected $BusinessModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->EmsModel = model('Ems');

        $this->BusinessModel = model('business.Business');
    }

    public function send()
    {
        if ($this->request->isAjax()) {

            $email = $this->request->param('email', '', 'trim');
            $busid = $this->request->param('busid', 0, 'trim');
            if (empty($email)) {
                $this->error('邮箱不能为空');
            }

            $business = $this->BusinessModel->find($busid);

            if ($business['auth'] == 1) {
                $this->error('您已认证了，无须重复认证');
            }

            // 开启事务
            $this->EmsModel->startTrans();

            // 先查询一下该邮箱过期验证码
            $DelStatus = $this->EmsModel->where(['email' => $email])->delete();

            if ($DelStatus === false) {
                $this->error('删除过期验证码失败');
            }

            // 实例化发送邮箱类
            $Email = new Email();

            // 生成随机验证码
            $code = build_randstr(6);

            // 定义邮件内容
            $content = "您的邮箱认证验证码：<b>{$code}</b>";

            // 需要把验证码存到数据表里
            $EmsData = [
                'event' => 'emailAuth', // 定义自定义事件名
                'email' => $email, // 接收人邮箱
                'code' => $code, // 验证码
                'times' => 1, // 验证次数
                'ip' => $this->request->ip(), // ip地址
            ];

            $EmsStatus = $this->EmsModel->validate('common/Ems')->save($EmsData);

            if ($EmsStatus === false) {

                // 新增验证码失败，告诉系统这边回滚前面操作成功的记录
                $this->EmsModel->rollback();

                // 返回错误信息
                $this->error($this->EmsModel->getError());
            }

            // 获取系统邮箱配置的发件人邮箱
            $FromEmail = config('site.mail_from');

            $result = $Email->subject('云平台认证')->from($FromEmail, '云平台')->message($content)->to($email)->send();

            if ($result === false) {
                $this->EmsModel->rollback();
                // $this->error("失败");
            }


            // 综合判断
            if ($DelStatus === false || $EmsStatus === false || $result === false) {
                // 回滚事务
                $this->EmsModel->rollback();
                $this->error('发送失败');
            } else {
                // 提交事件
                $this->EmsModel->commit();
                $this->success('发送成功');
            }
        }
    }


    public function email()
    {
        if ($this->request->isAjax()) {
            $code = $this->request->param('reset', '', 'trim');
            $email = $this->request->param('email', '', 'trim');
            $busid = $this->request->param('busid', 0, 'trim');

            // 验证码是否为空
            if (empty($code)) {
                $this->error('验证码不能为空');
            }

            // 查询数据表是否存在该条记录
            $ems = $this->EmsModel->where(['email' => $email, 'code' => $code, 'event' => 'emailAuth'])->find();

            if (empty($ems)) {
                $this->error('验证码错误');
            }

            // 开启事务
            $this->BusinessModel->startTrans();
            $this->EmsModel->startTrans();

            $BusinessData = [
                'id' => $busid,
                'auth' => 1
            ];

            $BusinessStatus = $this->BusinessModel->isUpdate(true)->save($BusinessData);

            if ($BusinessStatus === false) {
                $this->error('更新用户认证状态失败');
            }

            // 删除当前记录
            $EmsStatus = $this->EmsModel->destroy($ems['id']);

            if ($EmsStatus === false) {
                $this->BusinessModel->rollback();
                $this->error('删除验证码失败');
            }

            if ($BusinessStatus === false || $EmsStatus === false) {
                $this->BusinessModel->rollback();
                $this->EmsModel->rollback();
                $this->error('认证失败');
            } else {
                $this->BusinessModel->commit();
                $this->EmsModel->commit();
                $this->success('认证成功');
            }
        }
    }

    public function check()
    {
        if ($this->request->isAjax()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $auth = $this->request->param('auth', 0, 'trim');

            $BusinessData = [
                'id' => $busid,
                'auth' => $auth
            ];

            $BusinessCheck = $this->BusinessModel->find($BusinessData);

            if ($BusinessCheck) {
                $this->success("已验证");
            }else{
                $this->error("未验证");
            }
        }
    }
}
